package com.lianyu.wangyiyun.fragment;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.SearchView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout;
import com.lianyu.wangyiyun.R;
import com.lianyu.wangyiyun.databinding.FragmentMainBinding;
import com.lianyu.wangyiyun.utils.MyUtils;
import com.lianyu.wangyiyun.widget.MyTabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Chen Yuguang
 * @date 2021/5/14 22:09
 */
public class MainFragment extends AbstractLazyFragment {

    private final HistoryFragment historyFragment = new HistoryFragment();
    private final CollectFragment collectFragment = new CollectFragment();

    @Override
    protected View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        FragmentMainBinding fragmentMainBinding = FragmentMainBinding.inflate(inflater, container, false);
        SearchView searchView = fragmentMainBinding.searchView;
        //设置searchView点击时事件
        searchView.setOnClickListener(search -> {
            //获得fragment管理者
            FragmentManager fragmentManager = getFragmentManager();
            if (fragmentManager != null) {
                //新建事务
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                //隐藏本界面
                transaction.hide(this);
                //显示搜索界面
                transaction.add(R.id.fragmentContainer, new SearchFragment(), "search");
                //提交事务
                transaction.commit();
            }
        });
        Activity activity = getActivity();
        int statusHeight = 0;
        if (activity != null) {
            statusHeight = MyUtils.getStatusBarHeight(activity);
        }
        AppBarLayout appBarLayout = fragmentMainBinding.appBarLayout;
        MyUtils.setMargins(appBarLayout, 0, statusHeight, 0, 0);
        Toolbar toolbar = fragmentMainBinding.toolBar;
        //透明动画
        appBarLayout.addOnOffsetChangedListener((appBarLayout1, verticalOffset) -> {
            float alpha = 1 - ((float) -verticalOffset) / appBarLayout1.getTotalScrollRange();
            toolbar.setAlpha(alpha);
        });
        FrameLayout topLayout = fragmentMainBinding.topLayout;
        MyUtils.setSize(topLayout, statusHeight, 0);
        TabLayout tabLayout = fragmentMainBinding.tabLayout;
        ViewPager2 viewPager2 = fragmentMainBinding.viewPager2;
        List<Fragment> mFragments = new ArrayList<>();
        mFragments.add(new LocalFragment());
        mFragments.add(historyFragment);
        mFragments.add(new RecommendFragment());
        mFragments.add(collectFragment);
        //实例化适配器
        MyUtils.MyFragmentStateAdapter myAdapter = new MyUtils.MyFragmentStateAdapter(getActivity(), mFragments);
        //给ViewPager2设置适配器
        viewPager2.setAdapter(myAdapter);
        //ViewPager2设置预加载 传入大于1的值来设置预加载数量,默认不预加载
        viewPager2.setOffscreenPageLimit(4);
        String[] titles = {"本地", "历史", "推荐", "收藏"};
        int[] icons = {R.drawable.local, R.drawable.history, R.drawable.recommend, R.drawable.collect};
        new MyTabLayoutMediator(tabLayout, viewPager2, true, false, (tab, position) -> {
            // tab:当前处于选中状态的Tab对象
            // position:当前Tab所处的位置
            tab.setText(titles[position]);
            tab.setIcon(icons[position]);
        }).attach();
        return fragmentMainBinding.getRoot();
    }

    /**
     * 刷新历史记录
     *
     * @author Chen Yuguang
     * @date 2021/5/21 16:41
     */
    public void refreshHistory() {
        historyFragment.getRefreshLayout().autoRefresh();
    }

    /**
     * 刷新收藏列表
     *
     * @author Chen Yuguang
     * @date 2021/5/21 16:41
     */
    public void refreshCollect() {
        collectFragment.getRefreshLayout().autoRefresh();
    }

}
